<template>
  <el-card style="margin: 20px">
    <el-row type="flex" justify="space-between">
      <el-col :span="7"> </el-col>
      <el-col :span="2">
        <el-button type="success" class="el-icon-edit" @click="adduser"
          >新增用户</el-button
        >
      </el-col>
    </el-row>
    <el-row>
      <el-form :inline="true" label-position="right">
        <el-col :span="6">
          <el-form-item label="学科">
            <el-select v-model="value" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="二级目录">
            <el-select v-model="value" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="标签">
            <el-select v-model="value" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="关键字">
            <el-input placeholder="根据题干搜索"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <el-row>
      <el-form :inline="true" label-position="right">
        <el-col :span="6">
          <div>
            <el-form-item label="试题类型">
              <el-select v-model="value" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="6">
          <el-form-item label="难度">
            <el-select v-model="value" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="方向">
            <el-select v-model="value" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="录入人">
            <el-select v-model="value" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <el-row>
      <el-form :inline="true" label-position="right">
        <el-col :span="6">
          <el-form-item label="题目备注">
            <el-input></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="企业简称">
            <el-input></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="城市">
            <el-input></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="">
            <el-button style="margin-left: 10px">清空</el-button>
            <el-button type="primary" @click="search">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>

    <el-alert
      style="margin-top: 20px"
      :title="`共${counts}条记录`"
      type="info"
      :closable="false"
      show-icon
    >
    </el-alert>
    <!-- 表格 -->
    <el-table style="margin-top: 20px" :data="userlist">
      <el-table-column prop="number" label="试题编号" width="100">
      </el-table-column>
      <el-table-column prop="subject" label="学科" width="180">
      </el-table-column>
      <el-table-column prop="catalog" label="目录" width="180" />
      <el-table-column prop="questionType" label="题型" width="180">
      </el-table-column>
      <el-table-column prop="answer" label="题干" width="180">
      </el-table-column>
      <el-table-column prop="addDate" label="录入时间" width="180">
      </el-table-column>
      <el-table-column prop="difficulty" label="难度" width="180">
      </el-table-column>
      <el-table-column prop="creator" label="录入人" width="180">
      </el-table-column>
      <el-table-column prop="date" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            icon="el-icon-edit"
            circle
            type="primary"
            plain
            @click="changeuser(row)"
          ></el-button>
          <el-button
            icon="el-icon-delete"
            circle
            type="danger"
            plain
            @click="removeUser(row, row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block" style="margin: 25px">
      <el-pagination
        background
        @size-change="5"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage3"
        :page-sizes="[10, 20, 30, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
      >
      </el-pagination>
    </div>

    <!-- 弹框 -->

    <el-dialog
      @close="handleClose"
      :title="title"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <!-- 新增 -->
      <div v-if="addView">
        <el-form v-model="form" label-width="120px" ref="formRef">
          <el-form-item label="用户名">
            <el-input v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="form.email"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="form.password"></el-input>
          </el-form-item>
          <el-form-item label="角色">
            <el-input v-model="form.role"></el-input>
          </el-form-item>
          <el-form-item label="权限组名称">
            <el-select v-model="form.permission_group_id" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="联系电话">
            <el-input v-model="form.phone"></el-input>
          </el-form-item>
          <el-form-item label="介绍">
            <el-input
              type="textarea"
              v-model="form.introduction"
              placeholder="Please input"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>

      <!-- 修改 -->
      <div v-if="changeView">
        <el-form v-model="form" label-width="120px" ref="formRef">
          <el-form-item label="用户名">
            <el-input v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="form.email"></el-input>
          </el-form-item>

          <el-form-item label="角色">
            <el-input v-model="form.role"></el-input>
          </el-form-item>
          <el-form-item label="权限组名称">
            <el-select v-model="form.permission_group_id" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.username"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="联系电话">
            <el-input v-model="form.phone"></el-input>
          </el-form-item>
          <el-form-item label="介绍">
            <el-input
              type="textarea"
              v-model="form.introduction"
              placeholder="Please input"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import { list, add, remove } from "@/api/hmmm/questions";
import { simple } from "@/api/base/permissions";
export default {
  data() {
    return {
      value: "",
      id: "",
      changeView: false,
      title: "",
      addView: false,
      options: "",
      keyword: "",
      form: {
        avatar: null,
        email: "",
        password: "",
        username: "",
        phone: "",
        introduction: "",
        role: "",
        permission_group_id: "",
      },
      dialogVisible: false,
      currentPage3: null,
      username: "",
      pages: "",
      input: "",
      counts: null,
      userlist: [],
      params: {
        page: 1,
        pagesize: 10,
        username: "",
      },
    };
  },
  methods: {
    //列表
    async list() {
      const { data } = await list(this.params);
      console.log(data);
      this.userlist = data.items;
      this.counts = data.counts;
      this.pages = data.pages;
      console.log(this.userlist);
      this.userlist.forEach((item) => {
        if (item.questionType === "1") {
          item.questionType = "单选";
        } else if (item.questionType === '2') {
          item.questionType = "多选";
        } else {
          item.questionType = "简单";
        }
      });
      this.userlist.forEach((item) => {
        if (item.difficulty === '1') {
          item.difficulty = "简单";
        } else if (item.difficulty === '2') {
          item.difficulty = "一般";
        } else {
          item.difficulty = "困难";
        }
      });
    },
    //分页
    handleCurrentChange(current) {
      this.params.page = current;
      this.list();
    },
    //搜索
    async search() {
      await list(this.params);
      // console.log(1234565);
      this.list();
    },
    //添加
    async adduser() {
      this.dialogVisible = true;
      this.addView = true;
      this.changeView = false;
      this.title = "添加用户";
      const { data } = await simple();
      console.log(data);
      this.options = data;
    },
    //修改
    async changeuser(row) {
      console.log(row);
      this.form = row;
      this.id = row.id;
      this.dialogVisible = true;
      this.addView = false;
      this.changeView = true;
      this.title = "编辑用户";
      const { data } = await simple();
      this.options = data;
    },
    //删除
    async removeUser(row, id) {
      console.log(id);
      const res = await remove(row, id);
      this.$message.success("删除用户成功");
      console.log(res);
      this.list();
    },
    //清空页面
    handleClose() {
      this.dialogVisible = false;
      this.$refs.formRef.resetFields();
      this.form = {};
    },
    //弹框确认
    async submit() {
      console.log(111);
      if (this.title === "添加用户") {
        await add(this.form);
        this.$message.success("新增用户成功");
      } else {
        await list(this.form, this.id);
        this.$message.success("编辑用户成功");
      }
      this.dialogVisible = false;
      this.list();
    },
  },
  created() {
    this.list();
  },
};
</script>

<style scoped lang="scss">
.a {
  display: flex;
}
</style>
